import 'package:boss/models/cloud_printer.dart';
import 'package:boss/provider/cloud_printer_provider.dart';
import 'package:boss/ui/page/printer/page/printerStepTwo/printer_step_two_page.dart';
import 'package:boss/ui/page/printer/widget/printer_item.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class PrinterList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<CloudPrinterProvider>(
      builder: (context, provider, child) => LoaderPullRefresh<CloudPrinter>(
        refreshTask: provider.queryCloudPrinterList,
        builder: (context) => provider.cloudPrinters.length == 0
            ? EmptyWidget(title: '未有配置云打印机')
            : ListView.builder(
                itemBuilder: (context, index) {
                  var current = provider.cloudPrinters[index];

                  return PrinterItem(
                    data: current,
                    onTap: () {
                      Navigator.pushNamed(context, PrinterStepTwoPage.pageName, arguments: {
                        'id': current.id,
                      });
                    },
                    onDelete: () async {
                      var provider = Provider.of<CloudPrinterProvider>(
                        context,
                        listen: false,
                      );
                      provider.deleteCloudPrinter(current);
                    },
                  );
                },
                itemCount: provider.cloudPrinters.length,
              ),
      ),
    );
  }
}
